package com.qf.aliyun.pay;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;

import java.util.Date;


public class AlipayUtils {


    public static void main(String[] args) throws AlipayApiException {
        String orderSubject = "测试订单";
        String orderDesc = "订单描述信息";
        String totalAmount = "1";
        String orderNum = "202404031330002";

        // pc网站支付获取跳转支付宝收银台页面
//        String s = pcOrder(orderSubject, orderNum, orderDesc, totalAmount);
//        System.out.println(s);

        // 手机网站支付获取跳转页面
//        String s = wapOrder(orderSubject, orderNum, orderDesc, totalAmount);
//        System.out.println(s);

        // 获取支付二维码
//        String url = unifiedOrder(orderSubject, orderNum, orderDesc, totalAmount);
//        System.out.println(url);

//        System.out.println(queryOrder(orderNum));
    }

    /**
     * 统一下单接口，返回支付二维码的url
     *
     * @param orderNum
     * @throws AlipayApiException
     */
    //https://opendocs.alipay.com/open/f540afd8_alipay.trade.precreate?scene=19&pathHash=d3c84596
    public static String unifiedOrder(String orderSubject, String orderNum, String orderDesc, String totalAmount) throws AlipayApiException {

        String notifyUrl = "http//locahost:9216";

        AlipayTradePrecreateRequest req = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo(orderNum);
        model.setSubject(orderSubject); //订单标题
        model.setBody(orderDesc); //订单描述信息
        model.setTotalAmount(totalAmount);  //支付金额
//        model.setTimeExpire(DateUtil.format(payOrder.getExpiredTime(), DatePattern.NORM_DATETIME_FORMAT));  // 订单超时时间
        req.setNotifyUrl(notifyUrl); // 设置异步通知地址
        req.setBizModel(model);

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(PayConstants.appid);
        alipayConfig.setPrivateKey(PayConstants.privatekey);
        alipayConfig.setAlipayPublicKey(PayConstants.publickey);
        alipayConfig.setServerUrl(PayConstants.serverurl);
        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);
        AlipayTradePrecreateResponse response = client.execute(req);
//        System.out.println("将这个url转换为二维码图片即可支付："+response.getQrCode());
//        System.out.println(response.getBody());
        return response.getQrCode();
    }

    public static String queryOrder(String orderNum) throws AlipayApiException {

        AlipayTradeQueryRequest req = new AlipayTradeQueryRequest();

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(orderNum);
        req.setBizModel(model);


        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(PayConstants.appid);
        alipayConfig.setPrivateKey(PayConstants.privatekey);
        alipayConfig.setAlipayPublicKey(PayConstants.publickey);
        alipayConfig.setServerUrl(PayConstants.serverurl);
        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryResponse response = client.execute(req);
        System.out.println(response.getBody());
//        System.out.println("订单支付状态为:"+response.getTradeStatus());
        return response.getTradeStatus();
    }


    public static String wapOrder(String orderSubject, String orderNum, String orderDesc, String totalAmount) throws AlipayApiException {

        String notifyUrl = "http//locahost:9216";
        String returnUrl = "http://127.0.0.1:9216/api/pay/return/alipay";

        AlipayTradeWapPayRequest req = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(orderNum);
        model.setSubject(orderSubject); //订单标题
        model.setBody(orderDesc); //订单描述信息
        model.setTotalAmount(totalAmount);  //支付金额
        model.setTimeExpire(DateUtil.format(new Date(System.currentTimeMillis() + 1000 * 60 * 30), DatePattern.NORM_DATETIME_FORMAT));  // 订单超时时间
        model.setProductCode("QUICK_WAP_PAY");
        req.setNotifyUrl(notifyUrl); // 设置异步通知地址
        req.setReturnUrl(returnUrl); // 同步跳转地址
        req.setBizModel(model);

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(PayConstants.appid);
        alipayConfig.setPrivateKey(PayConstants.privatekey);
        alipayConfig.setAlipayPublicKey(PayConstants.publickey);
        alipayConfig.setServerUrl(PayConstants.serverurl);

        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);

        AlipayTradeWapPayResponse response = client.pageExecute(req, "GET");
//        System.out.println(response.getBody());
        return response.getBody();

    }


    public static String pcOrder(String orderSubject, String orderNum, String orderDesc, String totalAmount) throws AlipayApiException {
        String notifyUrl = "http//locahost:9216";
        String returnUrl = "http://127.0.0.1:9216/api/pay/return/alipay";
        AlipayTradePagePayRequest req = new AlipayTradePagePayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(orderNum);
        model.setSubject(orderSubject); //订单标题
        model.setBody(orderDesc); //订单描述信息
        model.setTotalAmount(totalAmount);  //支付金额
        model.setTimeExpire(DateUtil.format(new Date(System.currentTimeMillis() + 1000 * 60 * 30), DatePattern.NORM_DATETIME_FORMAT));  // 订单超时时间
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        req.setNotifyUrl(notifyUrl); // 设置异步通知地址
        req.setReturnUrl(returnUrl); // 同步跳转地址
        req.setBizModel(model);

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(PayConstants.appid);
        alipayConfig.setPrivateKey(PayConstants.privatekey);
        alipayConfig.setAlipayPublicKey(PayConstants.publickey);
        alipayConfig.setServerUrl(PayConstants.serverurl);

        DefaultAlipayClient client = new DefaultAlipayClient(alipayConfig);

        AlipayTradePagePayResponse response = client.pageExecute(req, "GET");
//        System.out.println(response.getBody());
        return response.getBody();
    }


}
